package cz.pproproj.aaservice.guimodule.config;
import java.util.Random;

public class StringUtil {

    private static final Random RANDOM = new Random();
    private static final String NUMBERS = "0123456789";
    private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String CHARACTERS = NUMBERS + LETTERS;

    /**
     * Generates random alphanumeric string of given length.
     *
     * @param length length of the string
     * @return random alphanumeric string
     */
    public static String randomAlphanumeric(int length) {
        return randomString(CHARACTERS, length);
    }

    /**
     * Generates random numeric string of given length.
     *
     * @param length length of the string
     * @return random numeric string
     */
    public static String randomNumbers(int length) {
        return randomString(NUMBERS, length);
    }

    private static String randomString(String allowedCharacters, int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            String randomCharacter = String.valueOf(allowedCharacters.charAt(RANDOM.nextInt(allowedCharacters.length())));
            randomCharacter = RANDOM.nextBoolean() ? randomCharacter.toLowerCase() : randomCharacter.toUpperCase();
            sb.append(randomCharacter);
        }
        return sb.toString();
    }
}